package www.way2inbox.adapter;


import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.way2sms.way2inbox.R;

import www.way2inbox.data.phonecontacts_model;

public class ContactsAdapter extends ArrayAdapter<phonecontacts_model>
		implements Filterable {
	private List<phonecontacts_model> contactList;
	private Context context;
	private Filter contactFilter;
	private List<phonecontacts_model> origcontactList;

	public ContactsAdapter(List<phonecontacts_model> contactList, Context ctx) {
		super(ctx, R.layout.adapter_contacts, contactList);		this.contactList = contactList;
		this.context = ctx;
		this.origcontactList = contactList;

	}

	public int getCount() {
		return contactList.size();
	}

	public phonecontacts_model getItem(int position) {
		return contactList.get(position);
	}

	public long getItemId(int position) {
		return contactList.get(position).hashCode();
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		PlanetHolder holder = new PlanetHolder();
		// First let's verify the convertView is not null
		if (convertView == null) {
			// This a new view we inflate the new layout
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.adapter_contacts, null);
			// Now we can fill the layout with the right values
			TextView tv = (TextView) v.findViewById(R.id.contact_name);
			TextView distView = (TextView) v.findViewById(R.id.contact_number);
			holder.planetNameView = tv;
			holder.distView = distView;
			v.setTag(holder);
		} else
			holder = (PlanetHolder) v.getTag();
		phonecontacts_model p = contactList.get(position);
		holder.planetNameView.setText(p.getName());
		holder.distView.setText("" + p.getPhoneno());
		return v;
	}

	private static class PlanetHolder {
		public TextView planetNameView;
		public TextView distView;
	}

	public void resetData() {
		contactList = origcontactList;
	}

	@Override
	public Filter getFilter() {
		if (contactFilter == null)
			contactFilter = new ContactFilter();
		return contactFilter;
	}

	private class ContactFilter extends Filter {
		@Override
		protected FilterResults performFiltering(CharSequence constraint) {
			FilterResults results = new FilterResults();
			// We implement here the filter logic
			if (constraint == null || constraint.length() == 0) {
				// No filter implemented we return all the list
				results.values = origcontactList;
				results.count = origcontactList.size();
			} else {
				// We perform filtering operation
				List<phonecontacts_model> nPlanetList = new ArrayList<phonecontacts_model>();
				for (phonecontacts_model p : contactList) {
					if (p.getName().toUpperCase()
							.startsWith(constraint.toString().toUpperCase()))
						nPlanetList.add(p);
					if (p.getPhoneno().startsWith(constraint.toString()))
						nPlanetList.add(p);
				}
				results.values = nPlanetList;
				results.count = nPlanetList.size();
			}
			return results;
		}

		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			// Now we have to inform the adapter about the new list filtered
			if (results.count == 0)
				notifyDataSetInvalidated();
			else {
				contactList = (List<phonecontacts_model>) results.values;
				notifyDataSetChanged();
			}
		}
	}
}

